package com.mdh.runerrands.controller;


import com.mdh.runerrands.Util.Result;
import com.mdh.runerrands.pojo.Address;
import com.mdh.runerrands.pojo.QueryPage;
import com.mdh.runerrands.service.AddressService;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/controller/address",name = "地址管理相关接口")
public class AddressController {
    @Resource
    private AddressService addressService;

    @ApiOperation(value = "通过id查询地址信息")
    @RequestMapping(value = "/findById",method = RequestMethod.GET)
    public Result findById(int id){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            Address address = addressService.findById(id);
            if (address != null){
                result.setData(address);
                result.setMessage("success");
            }else {
                result.setData(null);
                result.setMessage("查询的地址不存在");
            }
            return result;
        }catch (Exception e){
            result.setMessage("Exception");
            result.setData(e.getMessage());
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("通过ID删除地址信息")
    @RequestMapping(value = "deleteById",method = RequestMethod.GET)
    public Result deleteById(int id){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            boolean b = addressService.deleteById(id);
            if (b){
                result.setData(b);
                result.setMessage("success");
            }else {
                result.setData(b);
                result.setMessage("删除失败");
            }
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setStatus("500");
            result.setMessage("Exception");
            return result;
        }
    }
    @ApiOperation(value = "通过顾客ID查询地址信息")
    @RequestMapping(value = "/findByCustomerId",method = RequestMethod.GET)
    public Result findByCustomerId(int customerId){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            List<Address> address = addressService.findByCustomerId(customerId);
            if (address != null&& !address.isEmpty()){
                result.setData(address);
                result.setMessage("success");
            }else {
                result.setData(null);
                result.setMessage("查询的地址不存在");
            }
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }

    @ApiOperation(value = "查询地址和顾客信息")
    @RequestMapping(value = "/findAllAddressWithCustomer",method = RequestMethod.GET)
    public Result findAllAddressWithCustomer(){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            List allAddressWithCustomer = addressService.findAllAddressWithCustomer();
            result.setData(allAddressWithCustomer);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation(value = "保存或者更新地址信息")
    @RequestMapping(value = "/saveOrUpdate" ,method = RequestMethod.POST)
    public Result saveOrUpdate(Address address){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            if (address.getId() == 0){
                boolean insert = addressService.insert(address);
                if (insert){
                    result.setData(insert);
                    result.setMessage("success");
                }else {
                    result.setData(insert);
                    result.setMessage("保存失败");
                }
                return result;
            }else {
                boolean update = addressService.update(address);
                result.setData(update);
                if (update){
                    result.setMessage("success");
                }else {
                    result.setMessage("更新地址失败");
                }
                return result;
            }
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation(value = "批量删除地址")
    @RequestMapping(value = "/batchDelete",method = RequestMethod.POST)
    public Result batchDelete(int ...ids){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        try {
            if (ids.length == 0){
                return result;
            }
            boolean b = addressService.batchDelete(ids);
            result.setData(b);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
    @ApiOperation("分页查询")
    @RequestMapping(value = "/queryPage",method = RequestMethod.POST)
    public Result queryPage(int page,int pageSize,String province,String city,String area){
        Result result = new Result();
        result.setStatus("200");
        result.setTimestamp(String.valueOf(System.currentTimeMillis()));
        QueryPage queryPage = new QueryPage();
        try {
            List<Address> addresses = addressService.queryPage(page, pageSize, province, city, area);
            int i = addressService.selectCount(province, city, area);
            queryPage.setList(addresses);
            queryPage.setPage(page);
            queryPage.setPageSize(pageSize);
            queryPage.setTotal(i);
            result.setData(queryPage);
            result.setMessage("success");
            return result;
        }catch (Exception e){
            result.setData(e.getMessage());
            result.setMessage("Exception");
            result.setStatus("500");
            return result;
        }
    }
}
